package cn.mjnxin.iot.base.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.mjnxin.iot.base.util.MixUtil;
import io.netty.buffer.ByteBuf;

/**
 * 上行报文中转
 * @Description: 终端报文实际发送前置
 * @author CLG 2023年10月10日
 * @version V1.0.0
 */
public class LogWorker{
	private static final Logger MSG = LoggerFactory.getLogger("MSG");
	private static final Logger INFO = LoggerFactory.getLogger("INF");
	private static final Logger IPLOG = LoggerFactory.getLogger("HIS"); // 记录设备ip地址
	private static final Logger FAULT = LoggerFactory.getLogger("FAULT"); // 记录设备故障

    /**
     * 是否不打印日志
     */
    public static boolean logable = false;

    /**
     * 非强制模式是否可以记录日志
     * @return
     */
    public static boolean isLogable() {
        return logable;
    }
    
    public static void log(String logMsg) {
    	innerLog(null, logMsg, false);
    }

    public static void logForce(String logMsg, ByteBuf byteBuf) {
        innerLogForce(byteBuf, logMsg, true);
    }

    public static void log(String logMsg, ByteBuf byteBuf) {
        innerLog(byteBuf, logMsg, true);
    }

    public static void logForce(String logMsg) {
    	innerLogForce(null, logMsg, false);
    }
    
    public static void log(String logMsg, byte[] buf) {
        if (logable) {
            logging(logMsg, buf);
        }
    }
    
    public static void logForce(String logMsg, byte[] buf) {
        logging(logMsg, buf);
    }

    public static void logAddress(String deviceKey, String ipAddress) {
    	IPLOG.info(MixUtil.getCurrLogTime() + "|" + deviceKey + "|" + ipAddress);
    }
    
    public static void logFault(String deviceKey, String faultCause) {
    	FAULT.info(MixUtil.getCurrLogTime() + "|" + deviceKey + "|" + faultCause);
    }
    
    public static void error(String logMsg, Throwable t) {
    	INFO.error(logMsg, t);
    }

    public static void warn(String logMsg, Throwable t) {
    	INFO.warn(logMsg, t);
    }

    public static void error(String logMsg) {
    	INFO.error(logMsg);
    }

    public static void warn(String logMsg) {
    	INFO.warn(logMsg);
    }

    private static void innerLog(ByteBuf byteBuf, String logMsg, boolean isRelease) {
        if (logable) {
        	if (byteBuf != null) {
                byteBuf.readerIndex(0);
                byte[] dst = new byte[byteBuf.readableBytes()];
                byteBuf.readBytes(dst);
                byteBuf.readerIndex(0);
                logging(logMsg, dst);
        	}
        	else {
                logging(logMsg, null);
        	}
        }
    }

    private static void innerLogForce(ByteBuf byteBuf, String logMsg, boolean isRelease) {
        if (logable) {
        	if (byteBuf != null) {
                byteBuf.readerIndex(0);
                byte[] dst = new byte[byteBuf.readableBytes()];
                byteBuf.readBytes(dst);
                byteBuf.readerIndex(0);
                logging(logMsg, dst);
        	}
        	else {
                logging(logMsg, null);
        	}
        }
        else {
            logging(byteBuf, logMsg, isRelease);
        }
    }

    private static void logging(ByteBuf byteBuf, String logMsg, boolean isRelease) {
        if(byteBuf != null) {
            byteBuf.readerIndex(0);
            byte[] buf = new byte[byteBuf.readableBytes()];
            byteBuf.readBytes(buf);
            byteBuf.readerIndex(0);
            MSG.info(logMsg + buf.length + ":" + MixUtil.bytes2hex(buf));
        }
        else {
        	INFO.info(logMsg);
        }
    }

    private static void logging(String logMsg, byte[] buf) {
        if(buf != null) {
        	MSG.info(logMsg  + buf.length + ":" + MixUtil.bytes2hex(buf));
        }
        else {
        	INFO.info(logMsg);
        }
    }
}
